<template>
  <div>
    <van-search
      v-model="searchValue"
      placeholder="请输入搜索公司名称"
      shape="round"
      @search="onSearch"
      @cancel="onCancel"
    />
    <van-radio-group v-model="radio" direction="horizontal">
      <van-radio name="1" @click="radioClick('1')">未跟进</van-radio>
      <van-radio name="2" @click="radioClick('2')">已跟进</van-radio>
      <van-radio name="3" @click="radioClick('3')">我的</van-radio>
    </van-radio-group>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <zw-list
        v-for="(item, index) in listData"
        :key="index"
        pageName="questionBank"
        :configData="item"
        @goToDetail="goToDetail"
        @follow="follow"
      ></zw-list>
    </van-list>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userData: {},
      searchValue: "",
      queryData: {},
      radio: "0",
      listData: [
        // {
        //   title: "浙江润湾投资有限公司",
        //   status: 0,
        //   inserTime: "2020-08-13",
        //   question: "用能异常",
        // },
        // {
        //   title: "华立股份有限公司",
        //   status: 0,
        //   inserTime: "2020-08-01",
        //   question: "用能异常",
        // },
        // {
        //   title: "杭州一点通科技有限公司",
        //   status: 1,
        //   inserTime: "2020-07-20",
        //   question: "企业填报",
        // },
      ],
      loading: false,
      finished: false,
      queryData: {
        pageSize: 10,
        pageNo: 1,
        order:'insertTime',
        currole: "",
      },
      columns: [],
    };
  },
  created() {
    this.userData = JSON.parse(sessionStorage.getItem("userData"));
    // if (this.userData.roleNames.indexOf("一级") > -1) {
    //   this.queryData.currole = "yj";
    // } else if (this.userData.roleNames.indexOf("二级") > -1) {
    //   this.queryData.currole = "ej";
    // } else if (this.userData.roleNames.indexOf("三级") > -1) {
    //   this.queryData.currole = "sj";
    // }
    if (this.userData.roleNames.indexOf("领导") > -1) {
      this.queryData.currole = "yj,ej,sj";
    } else if (this.userData.roleNames.indexOf("网格") > -1) {
      this.queryData.currole = "yi,ej,sj";
    } else if (this.userData.roleNames.indexOf("园区") > -1) {
      this.queryData.currole = "sj";
      this.queryData.parkCode = this.userData.orgCode;
    }
    if (this.$route.query.type) this.queryData.type = this.$route.query.type;
    if (this.$route.query.insertTime)
      this.queryData.insertTime = this.$route.query.insertTime;
    if(sessionStorage.getItem('queryData')){
      let sessionQueryData = JSON.parse(sessionStorage.getItem('queryData'))
      for(var k in sessionQueryData){
        this.queryData[k] = sessionQueryData[k]
      }
    }
  },
  methods: {
    getList() {
      this.$api
        .dictionary(
          { method: "post", type: "getDictionaryByCode" },
          { code: "wtlx" }
        )
        .then((res) => {
          console.log("问题类型", res);
          res.data.data[0].children.forEach((x) => {
            this.columns.push({ text: x.name, id: x.id, code: x.code });
          });
          this.$api
            .problemBase({ method: "post", type: "datagrid" }, this.queryData)
            .then((res) => {
              console.log("res", res);
              res.data.data.map((x) => {
                ``;
                this.columns.forEach((y) => {
                  if (y.code == x.type) {
                    x.tag = y.text;
                  }
                });
              });
              this.listData.push(...res.data.data);
              if (this.listData.length == res.data.total)
                return (this.finished = true);
              this.queryData.pageNo++;
              this.loading = false;
            });
        });
    },
    issueTypeEvent() {
      this.$api
        .dictionary(
          { method: "post", type: "getDictionaryByCode" },
          { code: "wtlx" }
        )
        .then((res) => {
          console.log("问题类型", res);
          res.data.data[0].children.forEach((x) => {
            this.columns.push({ text: x.name, id: x.id, code: x.code });
          });
        });
    },
    onLoad() {
      this.getList();
    },
    onSearch() {
      this.queryData.pageNo = 1;
      this.queryData.enterpriseName = this.searchValue;
      this.finished = false;
      this.loading = true;
      this.listData = [];
      this.onLoad();
    },
    onCancel() {},
    goToDetail(title, id) {
      console.log("deal", title);
      this.$router.push({
        name: "questionDetail",
        params: { title: title, problemId: id },
      });
    },
    follow(title, id) {
      console.log("deal", title);
      this.$router.push({
        name: "questionDeal",
        params: { title: title, type: "deal", problemId: id },
      });
    },
    radioClick(e) {
      console.log(e, this.radio1);
      if (e == this.radio1) {
        this.radio = "0";
        this.radio1 = "0";
        this.queryData.sort = 'desc'
        this.queryData.dataType = ''
      } else {
        this.radio = e;
        this.radio1 = e;
        if(e==1){
          this.queryData.dataType = 'wgj'
          this.queryData.sort = 'asc'
        }else if(e==2){
          this.queryData.dataType = 'ygj'
          this.queryData.sort = 'desc'
        }else{
          this.queryData.dataType = 'wd'
          this.queryData.sort = 'desc'
        }
      }
      this.queryData.pageNo = 1;
      this.queryData.enterpriseName = this.searchValue;
      this.finished = false;
      this.loading = true;
      this.listData = [];
      this.getList()
    },
  },
  beforeRouteLeave (to, from, next) {
    if (to.name === 'home') {
      // from.meta.keepAlive = false
      if (this.$vnode && this.$vnode.data.keepAlive) {
        if (this.$vnode.parent && this.$vnode.parent.componentInstance && this.$vnode.parent.componentInstance.cache) {
          if (this.$vnode.componentOptions) {
            var key = this.$vnode.key == null ? this.$vnode.componentOptions.Ctor.cid + (this.$vnode.componentOptions.tag ? `::${this.$vnode.componentOptions.tag}` : '') : this.$vnode.key
            var cache = this.$vnode.parent.componentInstance.cache
            var keys = this.$vnode.parent.componentInstance.keys
            if (cache[key]) {
              if (keys.length) {
                var index = keys.indexOf(key)
                if (index > -1) {
                  keys.splice(index, 1)
                }
              }
              delete cache[key]
            }
          }
        }
        from.meta.keepAlive = true
      }
    } else {
      from.meta.keepAlive = true
    }
    next()
  }
};
</script>

<style scoped lang="less">
.van-radio-group{
  padding: 5px 10px;
}
</style>
